﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 出院取消 : Form
    {
        public 出院取消()
        {
            InitializeComponent();
        }

        private void button_qx_Click(object sender, EventArgs e)
        {
            this.Hide();
            
        }
    
        


        private void 出院取消_Load(object sender, EventArgs e)
        {


            string sql = "select LeaveHospital.HosNo,Bed.BedNo,PatName,PatSex,PatType,HosSurgerydate,HosDepartment,LeaveHospital.HosArea from LeaveHospital,Bed,Patient where LeaveHospital.HosNo=Patient.HosNo and LeaveHospital.BedNo=Bed.BedNo";


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void button_ss_Click(object sender, EventArgs e)
        {
            string sql = "select LeaveHospital.HosNo,Bed.BedNo,PatName,PatSex,PatType,HosSurgerydate,HosDepartment,HosArea from LeaveHospital,Bed,Patient where LeaveHospital.HosNo=Patient.HosNo and LeaveHospital.BedNo=Bed.BedNo";
            string i = tb_ZYNo.Text.Trim();
            if (i!= "")
                sql += " and LeaveHospital.HosNo='" + i + "'";
            else
                MessageBox.Show("搜索内容不能为空！");
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void button_qd_Click(object sender, EventArgs e)
        {
         
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                string a = Convert.ToString(this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                string b = Convert.ToString(this.dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                if (MessageBox.Show("确定要删除吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                    return;
                string ins = string.Format("insert into InHospital( HosNo, BedNo, HosIndate, HosDepartment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness,HosTotal,HosBalance,DocNo,NurNo,HosOfficeDate,HosArea)select HosNo, BedNo, HosIndate, HosDepartment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness,HosTotal,HosBalance,DocNo,NurNo,HosOfficeDate,HosArea from LeaveHospital where LeaveHospital.HosNo='" + a + "'");
                string deli = string.Format("delete from LeaveHospital where HosNo='" + a + "'");
                string upp = string.Format("update Patient set Condition='住院'where Patient.HosNo='" + a + "'");
                string upb = string.Format("update Bed set BedCondition='已用'where Bed.BedNo='" + b + "'");
                SqlCommand cmd = new SqlCommand(ins, conn);
                SqlCommand com = new SqlCommand(deli, conn);
               SqlCommand bed = new SqlCommand(upb, conn);
                SqlCommand cam = new SqlCommand(upp, conn);
                conn.Open();
                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                int m = Convert.ToInt32(com.ExecuteNonQuery());
               int x = Convert.ToInt32(bed.ExecuteNonQuery());
                int y = Convert.ToInt32(cam.ExecuteNonQuery());
                if (n == 1 && m == 1  && y == 1&&x==1)
                {
                    MessageBox.Show("出院取消成功！");
                }
                else
                {
                    MessageBox.Show("出院取消失败！");
                }
                DataGridViewSelectedRowCollection mRowlnfo = this.dataGridView1.SelectedRows;
                foreach (DataGridViewRow dataGridViewRow in mRowlnfo)
                {
                    this.dataGridView1.Rows.RemoveAt(dataGridViewRow.Index);
                    dataGridViewRow.Selected = false;
                }
                this.dataGridView1.Refresh();
            }
           
        }
    }
}
